package com.example.gymnasiumsystem.dao;

import com.example.gymnasiumsystem.enity.UserInfo;
import com.example.gymnasiumsystem.util.DBUtil;

import java.sql.*;

public class UserInfoDAO {

    // 添加用户
    public boolean addUser(UserInfo userInfo) {
        String sql = "INSERT INTO userInfo (username, password, isAdmin) VALUES (?, ?, ?)";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, userInfo.getUsername());
            statement.setString(2, userInfo.getPassword());
            statement.setInt(3, userInfo.getIsAdmin());
            return statement.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 删除用户
    public boolean deleteUser(int userId) {
        String sql = "DELETE FROM userInfo WHERE userId = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setInt(1, userId);
            return statement.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 根据 userId 查询用户
    public UserInfo getUserById(int userId) {
        String sql = "SELECT * FROM userInfo WHERE userId = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setInt(1, userId);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    UserInfo userInfo = new UserInfo();
                    userInfo.setUserId(resultSet.getInt("userId"));
                    userInfo.setUsername(resultSet.getString("username"));
                    userInfo.setPassword(resultSet.getString("password"));
                    userInfo.setAdmin(resultSet.getInt("isAdmin"));
                    return userInfo;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 更新用户信息
    public boolean updateUser(UserInfo userInfo) {
        String sql = "UPDATE userInfo SET username = ?, password = ?, isAdmin = ? WHERE userId = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, userInfo.getUsername());
            statement.setString(2, userInfo.getPassword());
            statement.setInt(3, userInfo.getIsAdmin());
            statement.setInt(4, userInfo.getUserId());
            return statement.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 根据用户名查询用户
    public UserInfo getUserByUsername(String username) {
        String sql = "SELECT * FROM userInfo WHERE username = ?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, username);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    UserInfo userInfo = new UserInfo();
                    userInfo.setUserId(resultSet.getInt("userId"));
                    userInfo.setUsername(resultSet.getString("username"));
                    userInfo.setPassword(resultSet.getString("password"));
                    userInfo.setAdmin(resultSet.getInt("isAdmin"));
                    return userInfo;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
